
## test
import maya.cmds as cmds
import os
import socket
import sys
import pickle
import glob
import maya.mel as mel

sys.path.append(r'\\192.168.131.222\kali')
logo = """

DDDDDDDDDDDDD                                                                       SSSSSSSSSSSSSSS                                      
D::::::::::::DDD                                                                  SS:::::::::::::::S                                     
D:::::::::::::::DD                                                               S:::::SSSSSS::::::S                                     
DDD:::::DDDDD:::::D                                                              S:::::S     SSSSSSS                                     
  D:::::D    D:::::D    ooooooooooo   nnnn  nnnnnnnn       ggggggggg   ggggg     S:::::S                eeeeeeeeeeee       ooooooooooo   
  D:::::D     D:::::D oo:::::::::::oo n:::nn::::::::nn    g:::::::::ggg::::g     S:::::S              ee::::::::::::ee   oo:::::::::::oo 
  D:::::D     D:::::Do:::::::::::::::on::::::::::::::nn  g:::::::::::::::::g      S::::SSSS          e::::::eeeee:::::eeo:::::::::::::::o
  D:::::D     D:::::Do:::::ooooo:::::onn:::::::::::::::ng::::::ggggg::::::gg       SS::::::SSSSS    e::::::e     e:::::eo:::::ooooo:::::o
  D:::::D     D:::::Do::::o     o::::o  n:::::nnnn:::::ng:::::g     g:::::g          SSS::::::::SS  e:::::::eeeee::::::eo::::o     o::::o
  D:::::D     D:::::Do::::o     o::::o  n::::n    n::::ng:::::g     g:::::g             SSSSSS::::S e:::::::::::::::::e o::::o     o::::o
  D:::::D     D:::::Do::::o     o::::o  n::::n    n::::ng:::::g     g:::::g                  S:::::Se::::::eeeeeeeeeee  o::::o     o::::o
  D:::::D    D:::::D o::::o     o::::o  n::::n    n::::ng::::::g    g:::::g                  S:::::Se:::::::e           o::::o     o::::o
DDD:::::DDDDD:::::D  o:::::ooooo:::::o  n::::n    n::::ng:::::::ggggg:::::g      SSSSSSS     S:::::Se::::::::e          o:::::ooooo:::::o
D:::::::::::::::DD   o:::::::::::::::o  n::::n    n::::n g::::::::::::::::g      S::::::SSSSSS:::::S e::::::::eeeeeeee  o:::::::::::::::o
D::::::::::::DDD      oo:::::::::::oo   n::::n    n::::n  gg::::::::::::::g      S:::::::::::::::SS   ee:::::::::::::e   oo:::::::::::oo 
DDDDDDDDDDDDD           ooooooooooo     nnnnnn    nnnnnn    gggggggg::::::g       SSSSSSSSSSSSSSS       eeeeeeeeeeeeee     ooooooooooo   
                                                                    g:::::g                                                              
                                                        gggggg      g:::::g                                                              
                                                        g:::::gg   gg:::::g                                                              
                                                         g::::::ggg:::::::g                                                              
                                                          gg:::::::::::::g                                                               
                                                            ggg::::::ggg                                                                 
                                                               gggggg                                                                    
                                                                                                                                                                               
                                                                                                                                                                               
UUUUUUUU     UUUUUUUU                   iiii                                                                                  iiii          tttt                               
U::::::U     U::::::U                  i::::i                                                                                i::::i      ttt:::t                               
U::::::U     U::::::U                   iiii                                                                                  iiii       t:::::t                               
UU:::::U     U:::::UU                                                                                                                    t:::::t                               
 U:::::U     U:::::Unnnn  nnnnnnnn    iiiiiiivvvvvvv           vvvvvvv eeeeeeeeeeee    rrrrr   rrrrrrrrr       ssssssssss   iiiiiiittttttt:::::tttttttyyyyyyy           yyyyyyy
 U:::::D     D:::::Un:::nn::::::::nn  i:::::i v:::::v         v:::::vee::::::::::::ee  r::::rrr:::::::::r    ss::::::::::s  i:::::it:::::::::::::::::t y:::::y         y:::::y 
 U:::::D     D:::::Un::::::::::::::nn  i::::i  v:::::v       v:::::ve::::::eeeee:::::eer:::::::::::::::::r ss:::::::::::::s  i::::it:::::::::::::::::t  y:::::y       y:::::y  
 U:::::D     D:::::Unn:::::::::::::::n i::::i   v:::::v     v:::::ve::::::e     e:::::err::::::rrrrr::::::rs::::::ssss:::::s i::::itttttt:::::::tttttt   y:::::y     y:::::y   
 U:::::D     D:::::U  n:::::nnnn:::::n i::::i    v:::::v   v:::::v e:::::::eeeee::::::e r:::::r     r:::::r s:::::s  ssssss  i::::i      t:::::t          y:::::y   y:::::y    
 U:::::D     D:::::U  n::::n    n::::n i::::i     v:::::v v:::::v  e:::::::::::::::::e  r:::::r     rrrrrrr   s::::::s       i::::i      t:::::t           y:::::y y:::::y     
 U:::::D     D:::::U  n::::n    n::::n i::::i      v:::::v:::::v   e::::::eeeeeeeeeee   r:::::r                  s::::::s    i::::i      t:::::t            y:::::y:::::y      
 U::::::U   U::::::U  n::::n    n::::n i::::i       v:::::::::v    e:::::::e            r:::::r            ssssss   s:::::s  i::::i      t:::::t    tttttt   y:::::::::y       
 U:::::::UUU:::::::U  n::::n    n::::ni::::::i       v:::::::v     e::::::::e           r:::::r            s:::::ssss::::::si::::::i     t::::::tttt:::::t    y:::::::y        
  UU:::::::::::::UU   n::::n    n::::ni::::::i        v:::::v       e::::::::eeeeeeee   r:::::r            s::::::::::::::s i::::::i     tt::::::::::::::t     y:::::y         
    UU:::::::::UU     n::::n    n::::ni::::::i         v:::v         ee:::::::::::::e   r:::::r             s:::::::::::ss  i::::::i       tt:::::::::::tt    y:::::y          
      UUUUUUUUU       nnnnnn    nnnnnniiiiiiii          vvv            eeeeeeeeeeeeee   rrrrrrr              sssssssssss    iiiiiiii         ttttttttttt     y:::::y           
                                                                                                                                                            y:::::y            
                                                                                                                                                           y:::::y             
                                                                                                                                                          y:::::y              
                                                                                                                                                         y:::::y               
                                                                                                                                                        yyyyyyy    

"""

RNDList = ['onetera','SUJIN' , 'ikkun' , 'cgh' , 'alien','calcium']    
MAYAinfo = {}
MAYAinfo['VERSION'] = cmds.about(v=1).split(' ')[0]
MAYAinfo['BIT'] = '64' if '64' in cmds.about(v=1) else '32'
MAYAinfo['ID'] = os.environ['USERNAME']
MAYAinfo['USERNAME'] = os.environ['USERNAME']
MAYAinfo['COMPUTERNAME'] = os.environ['COMPUTERNAME']
MAYAinfo['IP_ADDRESS'] = socket.gethostbyname(socket.gethostname())
#MAYAinfo['dataServer'] = r'\\'+SERVER_IP
MAYAinfo['userINIpath'] = 'y:\\INI\\' + MAYAinfo['ID']

if MAYAinfo['BIT'] == '64' :
    sys.path.append(r'\\192.168.131.222\kali\Lib\site-package\64')
else :
    sys.path.append(r'\\192.168.131.222\kali\Lib\site-package\32')

## DB connect
#try: import MySQLdb
#except : pass
#
#if not os.path.isdir(MAYAinfo['userINIpath']) :
#    os.mkdir(MAYAinfo['userINIpath'])
#DB = MySQLdb.connect(host=SERVER_IP , user='root',passwd='studio77',db='ds_user')
#cr = DB.cursor()
#
#cr.execute('select ID from user') ## For comparing existed ID
#IDlist = [x[0] for x in cr.fetchall()]
#
#if not MAYAinfo['ID'] in IDlist:
#    cr.execute(''' insert into user(ID,name,machine,dept,ip)
#    values('%(ID)s','%(USERNAME)s','%(COMPUTERNAME)s','%(DEPARTMENT)s','%(IP_ADDRESS)s' )
#    ''' % MAYAinfo)
#    DB.commit()
#    cr.close()
#    print "\n>>> User MAYAinfo was registered..."
#else :
#    cr.close()
#    print "\n>>> User MAYAinfo was already registered..."

print "\n>>> SOA library was set up.....OK!"
for x in MAYAinfo:
    print '\n>>> ',x,' : ',MAYAinfo[x]
print '\n'

##for setup department
TDlist=['Character_TD','chartdA','chartdB'] + RNDList
if os.environ['USERNAME'] in TDlist:
    print "\n>>> Mels Sourcing for Setup department\n"
    TDfolder = r'Y:\Mel'
    mels = glob.glob(TDfolder+'\\*.mel')
    error_log = ''    
    for x in mels:
        try :
            mel.eval("source \""+x.replace('\\','\\\\')+"\"")
            print "[",os.path.basename(x),"].....OK"
        except :
            error_log = '+++++++++++ Errors +++++++++++++++\n'
            error_log += "[" + os.path.basename(x) + "]"
    if error_log != '' :        
        print "\n" + error_log
        print '+++++++++++++++++++++++++++++++++\n'


if os.environ['USERNAME'] in RNDList:
    if os.environ['username'] == 'onetera' :
        sys.path.append(r'D:\WORK\kali\Project')
        sys.path.append(r'D:\WORK\kali\Python\Lib')
    elif os.environ['username'] == 'sdringo' :
        sys.path.append(r"C:\Users\sdringo\Desktop\kali\Project")
        sys.path.append(r"C:\Users\sdringo\Desktop\kali\Python\Lib")
    elif os.environ['username'] == 'ikkun' :
        sys.path.append(r"F:\Source\kali\Project")
        sys.path.append(r"F:\Source\kali\Python\Lib")
    elif os.environ['username'] == 'alien' :
        sys.path.append(r"C:\kali\Project")
        sys.path.append(r"C:\kali\Python\Lib")
    elif os.environ['username'] == 'calcium' :
        sys.path.append(r"C:\work\kali\Project")
        sys.path.append(r"C:\work\kali\Python\Lib") 
    else :
        sys.path.append(r'\\192.168.131.222\kali\Project')

#execfile(r"\\192.168.131.222\soa\lib\notice\notice.py")

location = "C:\\Users\\"+ os.environ['USERNAME'] +'\Documents\maya\projects\default'
if not os.path.isdir(location):
    location = "C:\\Users\\"+ os.environ['COMPUTERNAME'] +'\Documents\maya\projects\default'

    
    
##projectRule = {
##"offlineEdit":"scenes/edits",
##"scene":"scenes",
##"IGES":"data",
##"DXFexport":"data",
##"OBJexport":"data",
##"lights":"renderData/shaders",
##"mel":"mel",
##"particles":"particles",
##"audio":"sound",
##"RIBexport":"data",
##"RIB":"data",
##"depth":"renderData/depth",
##"autoSave":"autosave",
##"diskCache":"data",
##"sourceImages":"sourceimages",
##"animImport":"data",
##"movie":"data",
##"iprImages":"renderData/iprImages",
##"mayaAscii":"scenes",
##"textures":"textures",
##"aliasWire":"data",
##"mayaBinary":"scenes",
##"move":"data",
##"renderScenes":"renderScenes",
##"images":"images",
##"DXF":"data",
##"clips":"clips",
##"animExport":"data",
##"OBJ":"data",
##"templates":"assets"
##}
##
##cmds.workspace( cr =  location )
##cmds.workspace( location , o = 1 )
##for i,x in enumerate(projectRule):
##    cmds.workspace(fr=projectRule.items()[i])
##    cmds.workspace(u=1)
##cmds.workspace(s=1)
##cmds.workspace( dir=location)

try:
    cmds.commandPort( n='%s:6000' % MAYAinfo['IP_ADDRESS'] ,  cl=True )
except : pass
try:
    cmds.commandPort( n='%s:6000' % MAYAinfo['IP_ADDRESS'] )
except:pass    

print logo
